home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / VELENG10.ZIP / BUILDOB.C < prev    next >
C/C++ Source or Header  |  1997-07-27  |  5KB  |  184 lines

  1. // ****************************************************************************
  2. // *                                                                          *
  3. // *                      Velena Source Code V1.0                             *
  4. // *                   Written by Giuliano Bertoletti                         *
  5. // *       Based on the knowledged approach of Louis Victor Allis             *
  6. // *   Copyright (C) 1996-97 by Giuliano Bertoletti & GBE 32241 Software PR   *
  7. // *                                                                          *
  8. // ****************************************************************************
  9.  
  10. // Portable engine version.
  11. // read the README file for further informations.
  12.  
  13. // ==========================================================================
  14.  
  15.  
  16. #include <stdio.h>
  17.  
  18.  
  19. #include "connect4.h"
  20. #include "pnsearch.h"
  21. #include "proto.h"
  22.  
  23. #define DATABASEFILE "openbook.cn4"
  24. #define TEMPFILE     "tempship.$$1"
  25.  
  26. extern short nodeseq[7];
  27. extern unsigned short *myscreen;
  28.  
  29. char tbp[64];
  30.  
  31. struct ob_header_type {
  32.                       char string_id[8];
  33.                       short version;
  34.                       unsigned long date;
  35.                       unsigned long dataoffset[42];
  36.                       unsigned long sha_header[5],sha_data[5];
  37.                       };
  38.  
  39. struct tree_type {
  40.                  struct tree_type *parent,*lson,*rson;
  41.                  char position[14];
  42.                  };
  43.  
  44. short compare_ob_positions(char *p1,char *p2)
  45.     {
  46.     register short x;
  47.  
  48.     for(x=0;x<14;x++)
  49.         {
  50.         if     (p1[x]<p2[x]) return -1;
  51.         else if(p1[x]>p2[x]) return +1;
  52.         }
  53.  
  54.     return 0;
  55.     }
  56.  
  57. void insert_obook_tree(char *position,struct tree_type *curr_tree)
  58.     {
  59.     short cmp;
  60.  
  61.     cmp=compare_ob_positions(position,curr_tree->position);
  62.     if(cmp<0)
  63.         {
  64.         if(!curr_tree->lson)
  65.             {
  66.             curr_tree->lson=(struct tree_type *)malloc(sizeof(struct tree_type));
  67.             curr_tree->lson->parent=curr_tree;
  68.             curr_tree->lson->lson=NULL;
  69.             curr_tree->lson->rson=NULL;
  70.  
  71.             memcpy(curr_tree->lson->position,position,14);
  72.             }
  73.         else insert_obook_tree(position,curr_tree->lson);
  74.         }
  75.     else if(cmp>0)
  76.         {
  77.         if(!curr_tree->rson)
  78.             {
  79.             curr_tree->rson=(struct tree_type *)malloc(sizeof(struct tree_type));
  80.             curr_tree->rson->parent=curr_tree;
  81.             curr_tree->rson->lson=NULL;
  82.             curr_tree->rson->rson=NULL;
  83.  
  84.             memcpy(curr_tree->rson->position,position,14);
  85.             }
  86.         else insert_obook_tree(position,curr_tree->rson);
  87.         }
  88.  
  89.     return;
  90.     }
  91.  
  92. void flush_out_obtree(struct tree_type *node,FILE *h1)
  93.     {
  94.     if(!node) return;
  95.  
  96.     flush_out_obtree(node->lson,h1);
  97.     if(fwrite(node->position,1,14,h1)!=14)
  98.         fatal_error("Cannot write data to disk, probably insufficient free space");
  99.     flush_out_obtree(node->rson,h1);
  100.     }
  101.  
  102. struct tree_type *init_obook_tree()
  103.     {
  104.     struct tree_type *obroot;
  105.  
  106.     obroot=(struct tree_type *)malloc(sizeof(struct tree_type));
  107.     if(!obroot) return NULL;
  108.  
  109.     obroot->lson=NULL;
  110.     obroot->rson=NULL;
  111.     obroot->parent=NULL;
  112.  
  113.     return obroot;
  114.     }
  115.  
  116. void free_obook_tree(struct tree_type *current)
  117.     {
  118.     if(!current) return;
  119.  
  120.     free_obook_tree(current->lson);
  121.     free_obook_tree(current->rson);
  122.     free(current);
  123.     }
  124.  
  125. void build_white_opening_book()
  126.     {
  127.     struct tree_type *root;
  128.     short bb[64],x,value;
  129.     unsigned char blk[14],tp[64],t[64];
  130.     long size,posit=0,wps=0,bps=0;
  131.     FILE *h1,*h2;
  132.  
  133.     root=init_obook_tree();
  134.     if(!root) fatal_error("Not enough memory, darn!");
  135.  
  136.     h1=fopen(DATABASEFILE,"rb");
  137.     if(!h1) fatal_error("Data Base error!");
  138.  
  139.     size=fileln(h1);
  140.     if(size%14!=0) fatal_error("Opening book file is corrupted");
  141.  
  142.     printf("Loading and sorting opening book...\n");
  143.  
  144.     while(size>0)
  145.         {
  146.         fread(blk,1,12,h1);
  147.         expand_block(blk,t);
  148.  
  149.         for(x=0;x<64;x++)
  150.             bb[x]=t[x];
  151.  
  152.         get_lower(bb,tp);
  153.         collapse_position(tp,blk);
  154.  
  155.         blk[12]=getc(h1);
  156.         blk[13]=getc(h1);
  157.  
  158.         if(posit>0) insert_obook_tree(blk,root);
  159.         else memcpy(root->position,blk,14);
  160.  
  161.         if(blk[13]==0) wps++;
  162.         else bps++;
  163.  
  164.         posit++;
  165.         size-=14;
  166.         }
  167.  
  168.     fclose(h1);
  169.  
  170.     printf("Flushing out ordered opening book...\n");
  171.  
  172.     h2=fopen(WHITE_BOOK,"wb");
  173.     if(!h2) fatal_error("Cannot write output file!");
  174.  
  175.     flush_out_obtree(root,h2);
  176.  
  177.     free_obook_tree(root);
  178.     fclose(h2);
  179.  
  180.     printf("Positions for white %ld; positions for black %ld\n\n",wps,bps);
  181.     }
  182.  
  183.  
  184.